<template>
  <div>

  </div>
</template>

<script>
  import reachBottom from "../../mixins/reachBottom";

  export default {
    async asyncData({app, params}) {
      let tagList = []
      if (!params.title || params.title === 'subscribed') {
        tagList = await app.$api.getTagBySubscribed().then(res => res.s === 1 ? res.d.tagList : [])
      } else if (params.title === 'all') {
        tagList = await app.$api.getTagByAll({
          type: 'hot',
          page: 1, pageSize: 40
        }).then(res => res.s === 1 ? res.d.tags : [])
      }
      return {
        tagList
      }
    },
    head() {
      return {
        title: `${this.currentNavItem.name ? this.currentNavItem.name + ' - ' : ''}掘金`
      }
    },
    layout: 'default-white',
validate({params}){
   const  whiteList = ['all','subscribed']
  if (params.title === undefined || whiteList.includes(params.title)){
    return true
  }
  return  false
},
    mixins: [reachBottom],
    data() {
      return {
        page: 1,
        pageSize: 40,
        keyword: '',
        isSearch: false,
        type: 'hot',
        currentNavItem: {},
        tagList: []
      }
    },
    methods: {
      reachBottom(){
        this.page++
        this.isSearch ? this.getTagBySearch({ isLoadMore:true}):this.getTagByAll({isLoadMore:true})
      },
      typeChange(type){
        if (this.type == type){
          return
        }
        this.type = type
        this.page = 1
        this.isSearch ? this.getTagBySearch() : this.getTagByAll()
      },
      tagSearch(){
        this.page = 1
        if (this.keyword){
          this.isSearch = true
          this.getTagBySearch()
        }else  {
          this.isSearch = false
          this.getTagByAll()
        }
      },
      async getTagByAll({isLoadMore = false} ={}){
        if (this.$route.params.title === 'all'){
          let res = await this.$api.getTagByAll(
            {
              type:this.type,
              page:this.page,
              pageSize:this.pageSize
            }
          )
          if (res.s === 1){
            this.tagList = isLoadMore ? this.tagList.concat(res.d.tags) : res.d.tags
          }
        }
      },
      async getTagBySearch({ isLoadMore = false} = {}){
        if (this.$route.params.title === 'all'){
          let res = await  this.$api.getTagBySearch({
            keyword:this.keyword,
            type:this.type,
            page:this.page,
            pageSize:this.pageSize
          })
          if (res.s === 1) {
            this.tagList = isLoadMore ? this.tagList.concat(res.d.tags) : res.d.tags
          }
        }
      }

    }
  }
</script>

<style lang="scss" scoped>

</style>
